﻿using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace StrategyGame
{
    class TaskBar
	{
		#region Static resources
		private static SpriteFont Font;
        private static SpriteFont ButtonFont;
        private static Texture2D MapButtonTexture;
        private static Texture2D CityButtonTexture;

        public static readonly int Height = 48;

		public static void LoadContent(ContentManager content)
		{
			MapButtonTexture = content.Load<Texture2D>("ui/button_map");
			CityButtonTexture = content.Load<Texture2D>("cities/city");

			Font = content.Load<SpriteFont>("fonts/SmallFont");
			ButtonFont = content.Load<SpriteFont>("fonts/TinyFont");
		}

		#endregion

		#region Buttons

		private struct Button
        {
            public Texture2D Icon;
            public string Text;

            public Button(Texture2D icon, string text)
            {
                Icon = icon;
                Text = text;
            }
        }

        List<Button> Buttons = new List<Button>();

		#endregion

		#region Rendering
		
		public void Draw(SpriteBatch sprites, GraphicsDevice device, Nation player_nation, StrategyGame.DisplayMode current_display)
        {
            bool top_align = false;

            Rectangle bar_rect = new Rectangle();
            bar_rect.X = 0;
            bar_rect.Width = device.ScissorRectangle.Width - SideBar.Width;
            bar_rect.Height = Height;
            bar_rect.Y = top_align? 0 : device.ScissorRectangle.Height - Height;

            Color bar_color = Color.Multiply(player_nation.NationalColor, 0.5f);
            bar_color.A = 255;

            SpriteRectangle.Draw(sprites, bar_rect, bar_color);

            //draw the buttons
            int gap_between_buttons = 4;
            int button_offset = gap_between_buttons;
            for (int button_it = 0; button_it < Buttons.Count; ++button_it)
            {
                Button button = Buttons[button_it];

                Rectangle button_rect = new Rectangle();
                button_rect.X = button_offset;
                button_rect.Width = Height; //buttons are square - make them the same size as the taskbar's height
                button_rect.Y = bar_rect.Y;
                button_rect.Height = Height;

                Color button_color;
                if ((int)current_display == button_it)
                    button_color = Color.White;
                else
                    button_color = Color.Gray;

                //F1, F2 etc
                string keybind_text = "F" +(button_it + 1).ToString();
                Vector2 keybind_text_pos = TextUtils.GetPositionForAlignedText(ButtonFont, keybind_text, button_rect, TextUtils.HorizontalAlignment.Left, TextUtils.VerticalAlignment.Top);

                Vector2 button_text_pos = TextUtils.GetPositionForAlignedText(ButtonFont, button.Text, button_rect, TextUtils.HorizontalAlignment.Right, TextUtils.VerticalAlignment.Bottom);

                sprites.Draw(button.Icon, button_rect, button_color);

                TextUtils.DrawWithDropShadow(sprites, ButtonFont, keybind_text, keybind_text_pos, Color.White, Color.Black, 1);
                TextUtils.DrawWithDropShadow(sprites, ButtonFont, button.Text, button_text_pos, Color.White, Color.Black, 1);

                button_offset += button_rect.Width + gap_between_buttons;
            }
        }

		#endregion

		public TaskBar()
        {
            Buttons.Add(new Button(MapButtonTexture, "Map"));
            Buttons.Add(new Button(CityButtonTexture, "City"));
			Buttons.Add(new Button(CityButtonTexture, "Research"));
        }
    }
}
